home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH11 / SRC / OBJSOL1.CLS < prev    next >
Text File  |  1996-03-26  |  8KB  |  309 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "ObjSolid"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. Option Explicit
  9.  
  10. ' These ObjPolygon objects are the oriented faces.
  11. Public Faces As New Collection
  12. Public Convex As Boolean
  13. Public MaxZ As Single
  14. ' ***********************************************
  15. ' Clip faces.
  16. ' ***********************************************
  17. Public Sub ClipEye(r As Single)
  18. Dim obj As Object
  19.  
  20.     For Each obj In Faces
  21.         obj.ClipEye r
  22.     Next obj
  23. End Sub
  24.  
  25.  
  26. ' ***********************************************
  27. ' Create faces to make a pyramid of height L with
  28. ' base given by the coord array.
  29. ' ***********************************************
  30. Sub Stellate(L As Single, ParamArray coord() As Variant)
  31. Dim x0 As Single
  32. Dim y0 As Single
  33. Dim z0 As Single
  34. Dim x1 As Single
  35. Dim y1 As Single
  36. Dim z1 As Single
  37. Dim x2 As Single
  38. Dim y2 As Single
  39. Dim z2 As Single
  40. Dim x3 As Single
  41. Dim y3 As Single
  42. Dim z3 As Single
  43. Dim Ax As Single
  44. Dim Ay As Single
  45. Dim Az As Single
  46. Dim Bx As Single
  47. Dim By As Single
  48. Dim Bz As Single
  49. Dim nx As Single
  50. Dim ny As Single
  51. Dim nz As Single
  52. Dim num As Integer
  53. Dim i As Integer
  54. Dim pt As Integer
  55.  
  56.     num = (UBound(coord) + 1) \ 3
  57.     If num < 3 Then
  58.         Beep
  59.         MsgBox "Must have at least 3 points to stellate.", , vbExclamation
  60.         Exit Sub
  61.     End If
  62.     
  63.     ' (x0, y0, z0) is the center of the polygon.
  64.     x0 = 0
  65.     y0 = 0
  66.     z0 = 0
  67.     pt = 0
  68.     For i = 1 To num
  69.         x0 = x0 + coord(pt)
  70.         y0 = y0 + coord(pt + 1)
  71.         z0 = z0 + coord(pt + 2)
  72.         pt = pt + 3
  73.     Next i
  74.     x0 = x0 / num
  75.     y0 = y0 / num
  76.     z0 = z0 / num
  77.     
  78.     ' Find the normal.
  79.     x1 = coord(0)
  80.     y1 = coord(1)
  81.     z1 = coord(2)
  82.     x2 = coord(3)
  83.     y2 = coord(4)
  84.     z2 = coord(5)
  85.     x3 = coord(6)
  86.     y3 = coord(7)
  87.     z3 = coord(8)
  88.     Ax = x2 - x1
  89.     Ay = y2 - y1
  90.     Az = z2 - z1
  91.     Bx = x3 - x2
  92.     By = y3 - y2
  93.     Bz = z3 - z2
  94.     m3Cross nx, ny, nz, Ax, Ay, Az, Bx, By, Bz
  95.     
  96.     ' Give the normal length L.
  97.     m3SizeVector L, nx, ny, nz
  98.     
  99.     ' The normal + <x0, y0, z0> gives the point.
  100.     x0 = x0 + nx
  101.     y0 = y0 + ny
  102.     z0 = z0 + nz
  103.  
  104.     ' Build the triangles that make up the solid.
  105.     x1 = coord(3 * num - 3)
  106.     y1 = coord(3 * num - 2)
  107.     z1 = coord(3 * num - 1)
  108.     pt = 0
  109.     For i = 1 To num
  110.         x2 = coord(pt)
  111.         y2 = coord(pt + 1)
  112.         z2 = coord(pt + 2)
  113.         AddFace x1, y1, z1, x2, y2, z2, x0, y0, z0
  114.         x1 = x2
  115.         y1 = y2
  116.         z1 = z2
  117.         pt = pt + 3
  118.     Next i
  119. End Sub
  120.  
  121. ' ***********************************************
  122. ' Add an oriented face to the solid.
  123. ' ***********************************************
  124. Public Sub AddFace(ParamArray coord() As Variant)
  125. Dim pgon As ObjPolygon
  126. Dim num As Integer
  127. Dim pt As Integer
  128. Dim i As Integer
  129.  
  130.     num = (UBound(coord) + 1) \ 3
  131.     If num < 3 Then
  132.         Beep
  133.         MsgBox "Faces in a Solid must contain at least 3 points.", , vbExclamation
  134.         Exit Sub
  135.     End If
  136.     
  137.     Set pgon = New ObjPolygon
  138.     Faces.Add pgon
  139.     
  140.     pt = 0
  141.     For i = 1 To num
  142.         pgon.AddPoint (coord(pt)), (coord(pt + 1)), (coord(pt + 2))
  143.         pt = pt + 3
  144.     Next i
  145. End Sub
  146.  
  147. ' ************************************************
  148. ' Perform backface removal on the faces.
  149. ' ************************************************
  150. Public Sub Cull(X As Single, Y As Single, z As Single)
  151. Dim obj As Object
  152.     
  153.     For Each obj In Faces
  154.         obj.Cull X, Y, z
  155.     Next obj
  156. End Sub
  157. ' ***********************************************
  158. ' Create normals for polygon objects.
  159. ' ***********************************************
  160. Sub CreateNormal(Objects As Collection)
  161. Dim obj As Object
  162.  
  163.     For Each obj In Faces
  164.         obj.CreateNormal Objects
  165.     Next obj
  166. End Sub
  167.  
  168. ' ***********************************************
  169. ' Set or clear the Culled property for all faces.
  170. ' ***********************************************
  171. Property Let Culled(value As Boolean)
  172. Dim obj As Object
  173.  
  174.     For Each obj In Faces
  175.         obj.Culled = value
  176.     Next obj
  177. End Property
  178.  
  179.  
  180.  
  181. ' ***********************************************
  182. ' Return a string indicating the object type.
  183. ' ***********************************************
  184. Property Get ObjectType() As String
  185.     ObjectType = "SOLID"
  186. End Property
  187.  
  188.  
  189. ' ************************************************
  190. ' Draw the object into a metafile.
  191. ' ************************************************
  192. Public Sub MakeWMF(mhdc As Integer)
  193. Dim obj As Object
  194.  
  195.     For Each obj In Faces
  196.         obj.MakeWMF mhdc
  197.     Next obj
  198. End Sub
  199.  
  200. ' ***********************************************
  201. ' Fix the data coordinates at their transformed
  202. ' values.
  203. ' ***********************************************
  204. Public Sub FixPoints()
  205. Dim obj As Object
  206.  
  207.     For Each obj In Faces
  208.         obj.FixPoints
  209.     Next obj
  210. End Sub
  211.  
  212. ' ************************************************
  213. ' Apply a transformation matrix which may not
  214. ' contain 0, 0, 0, 1 in the last column to the
  215. ' object.
  216. ' ************************************************
  217. Public Sub ApplyFull(M() As Single)
  218. Dim obj As Object
  219.  
  220.     For Each obj In Faces
  221.         obj.ApplyFull M
  222.     Next obj
  223. End Sub
  224.  
  225. ' ************************************************
  226. ' Apply a transformation matrix to the object.
  227. ' ************************************************
  228. Public Sub Apply(M() As Single)
  229. Dim obj As Object
  230.  
  231.     For Each obj In Faces
  232.         obj.Apply M
  233.     Next obj
  234. End Sub
  235.  
  236.  
  237. ' ************************************************
  238. ' Apply a nonlinear transformation.
  239. ' ************************************************
  240. Public Sub Distort(D As Object)
  241. Dim obj As Object
  242.  
  243.     For Each obj In Faces
  244.         obj.Distort D
  245.     Next obj
  246. End Sub
  247.  
  248. ' ************************************************
  249. ' Write a polyline to a file using Write.
  250. ' Begin with "SOLID" to identify this object.
  251. ' ************************************************
  252. Public Sub FileWrite(filenum As Integer)
  253. Dim obj As Object
  254.  
  255.     Write #filenum, "SOLID", Convex, Faces.Count
  256.     
  257.     For Each obj In Faces
  258.         obj.FileWrite filenum
  259.     Next obj
  260. End Sub
  261.  
  262.  
  263. ' ************************************************
  264. ' Draw the transformed solid on a Form, Printer,
  265. ' or PictureBox.
  266. ' ************************************************
  267. Public Sub Draw(canvas As Object, Optional r As Variant)
  268. Dim obj As Object
  269.  
  270.     For Each obj In Faces
  271.         obj.Draw canvas, r
  272.     Next obj
  273. End Sub
  274.  
  275. ' ************************************************
  276. ' Read a polyline from a file using Input.
  277. ' Assume the "SOLID" label has already been
  278. ' read.
  279. ' ************************************************
  280. Public Sub FileInput(filenum As Integer)
  281. Dim num As Integer
  282. Dim i As Integer
  283. Dim obj As Object
  284. Dim obj_type As String
  285.  
  286.     ' Read the number of faces in the solid.
  287.     Input #filenum, Convex, num
  288.     
  289.     ' Read faces from the file.
  290.     For i = 1 To num
  291.         Input #filenum, obj_type
  292.         Select Case obj_type
  293.             Case "SOLID"
  294.                 Set obj = New ObjSolid
  295.             Case "POLYGON"
  296.                 Set obj = New ObjPolygon
  297.             Case Else
  298.                 Beep
  299.                 MsgBox "Invalid Solid sub-object type """ & obj_type & """.", , vbExclamation
  300.                 Exit Sub
  301.         End Select
  302.         obj.FileInput filenum
  303.         Faces.Add obj
  304.     Next i
  305. End Sub
  306.  
  307.  
  308.  
  309.